<template>

  <el-form ref="form" :model="loginUser" :rules="rules" label-width="80px" class="loginForm" label-position="left">
    <el-button type="text" style="color: black" @click="$router.push('/login')">
      <el-icon>
        <Back/>
      </el-icon>&nbsp;返回
    </el-button>
    <h3 class="login_title">注册</h3>
    <el-form-item label="账号" prop="username">
      <el-input v-model="loginUser.username" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password" >
      <el-input v-model="loginUser.password" type="password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPassword">
      <el-input v-model="loginUser.checkPassword" type="password" placeholder="请再次输入密码"></el-input>
    </el-form-item>
    <el-form-item style="width: 100%;margin-bottom: 0 ">
      <el-button type="primary" style="width: 100%;background: #505458;border: none" @click="register()">注册</el-button>
    </el-form-item>
  </el-form>
</template>


<script lang="ts">
import {reactive, getCurrentInstance, ref} from 'vue'
import {useRouter} from 'vue-router'
import {Back} from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  name: 'Login',
  components: {Back},
  setup() {
    const checked = ref(false);
    // @ts-ignore
    const {proxy} = getCurrentInstance()
    // 表单字段
    const loginUser = reactive({
      username: '',
      password: '',
      checkPassword: ''
    })

    //登录表单校验
    const rules = reactive({
      username: [
        {required: true, message: '请输入用户名', trigger: 'blur'},
        {min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur'}
      ],
      password: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
      ],
      checkPassword: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
      ]
    })

    const router = useRouter()

    const register = () => {
        request.post('/user/register', loginUser).then(res => {
          if (res.code === '0') {
            proxy.$message({
              message: '注册成功',
              type: 'success'
            })
            router.push('/login')
          } else {
            proxy.$message({
              message: res.msg,
              type: 'error'
            })
          }
        })
    }

    return {loginUser, rules, register, checked}
  },
}
</script>

<style>
.loginForm {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 35px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

</style>
